﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JA.Negocios;
using System.IO;
using JA.Negocios.Cadastros;

namespace JA.Web.Apresentacao
{
    public partial class DownFoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            int? idImovelXFoto = null;
            int? idImovel= null;

            if (Request["idImovelXFoto"] !=null)
            {
                idImovelXFoto = int.Parse(Request["idImovelXFoto"]);
            }
            else if (Request["idImovel"] != null)
            {
                idImovel = int.Parse(Request["idImovel"]);
            }

            string tipo = Request["type"];
            if (tipo==null) tipo="small";

            //Response.ContentType = "image/jpeg";

            //Recupera dados
            FotoBO fotoBO = new FotoBO();
            List<imovelxfotos> fotoVOs = fotoBO.RecuperaFotos(idImovelXFoto, idImovel);
            if (fotoVOs!=null && fotoVOs.Count > 0)
            {
                imovelxfotos fotoVO = fotoVOs[0];

                Response.ContentType = fotoVO.Mime;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fotoVO.Arquivo);
                if (fotoVO != null)
                {
                    if (tipo.Equals("extralarge"))
                    {
                        Response.BinaryWrite(fotoVO.Foto);
                        return;
                    }
                    if (tipo.Equals("large"))
                    {
                        Response.BinaryWrite(fotoVO.FotoLarge ?? fotoVO.Foto);
                        return;
                    }
                    if (tipo.Equals("medium"))
                    {
                        Response.BinaryWrite(fotoVO.FotoMedium ?? fotoVO.Foto);
                        return;
                    }
                    if (tipo.Equals("small"))
                    {
                        Response.BinaryWrite(fotoVO.FotoSmall ?? fotoVO.Foto);
                        return;
                    }
                }
            }

            Response.ContentType = "image/png";
            string filePath = HttpContext.Current.Server.MapPath("~/App_Themes/Padrao/imagens/backoffice/SemFoto.png");
            Response.BinaryWrite(File.ReadAllBytes(filePath));
        }
    }
}
